package com.gzyj.jtt808.v2011.additionalinfo;



import com.gzyj.jtt808.utils.HexBytesUtil;
import com.gzyj.jtt808.v2011.enums.addtionalinfo.JT808_Addtional_Position;
import io.netty.buffer.ByteBuf;
import lombok.Data;

import java.io.Serializable;

/**
 * 超速报警附加信息
 *
 * @author lhj
 */
@Data
public class JTT808_0x0200_0x11 extends JTT808_AddtionalInfo implements Serializable {

    private JT808_Addtional_Position PositionType;
    private Integer AreaId;//区域或路段ID

    public JTT808_0x0200_0x11() {
        this.setID(0x11);
    }

    @Override
    public int getLength(){
        if(this.PositionType== JT808_Addtional_Position.无特定位置){
            return 1;
        }else{
            return 5;
        }
    }

    @Override
    public void encode(ByteBuf buf) {
        buf.writeByte(this.getID());
        if (PositionType == JT808_Addtional_Position.无特定位置) {
            buf.writeByte(1);//写入长度
            buf.writeByte(this.PositionType.getCode());
        }else{
            buf.writeByte(5);
            buf.writeByte(this.PositionType.getCode());
            buf.writeInt(this.AreaId);
        }
    }

    @Override
    public JTT808_0x0200_0x11 decode(ByteBuf buf) {
        byte[] temp= HexBytesUtil.readByOffset(buf,1);
        this.setLength(Integer.parseInt(HexBytesUtil.bytesToHex(temp),16));

        if(this.getLength()==1){
            temp=HexBytesUtil.readByOffset(buf,1);
            this.PositionType=JT808_Addtional_Position.find(Integer.parseInt(HexBytesUtil.bytesToHex(temp),16));
        }else{
            temp=HexBytesUtil.readByOffset(buf,1);
            this.PositionType=JT808_Addtional_Position.find(Integer.parseInt(HexBytesUtil.bytesToHex(temp),16));
            temp=HexBytesUtil.readByOffset(buf,4);
            this.AreaId=HexBytesUtil.byteArrayToInt(temp);
        }


        return this;
    }
}
